package com.tnsw.facebookautoliker;

import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.UIUtils;
import com.tnsw.facebookautoliker.helper.GeneralManager;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.test.UiThreadTest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class StandardUpdateView implements ListViewDragInterface, OnClickListener, Runnable {

	private final String T_UPDATE = "Pull down to update";
	private final String T_RELEASE = "Release to update";
	private final String T_UPDATING = "Updating";
	
	private LayoutInflater inflater = null;
	protected String tag = "StandardUpdateView";
	private Context context;
	
	private ViewGroup root;
	private TextView tvStdUpdateText;
	private LinearLayout llSpace;
	private View parentView;
	private ProgressBar pgStdUpdate;
	private ImageView imgDown;
	
	private int bufferPx = 0;
	private int currentStage = 0;
	/**
	 * 0 = Pull down to update
	 * 1 = Release to update
	 * 2 = Updating..
	 */
	
	private int dp2ChangeStatus = 30;  // move down 30dp before changing text
	private int px;
	private int topDPwhenUpdating = 10;
	private ListViewDragExtender dragExtender;
	private ListViewDragActionListener actionListener;
	private boolean cancelOnClick = true;
	
	
	private Bitmap[] bmpIcon;
	
	public StandardUpdateView(Context context, View parentView) {
		this.context = context;
		this.parentView = parentView;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		this.bmpIcon = new Bitmap[2];
		this.bmpIcon[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.av_download);
		this.bmpIcon[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.av_upload);
		
		this.px = UIUtils.dp2px(this.context.getResources().getDisplayMetrics().density, this.dp2ChangeStatus);
	}
	
	public void setListViewDragActionListener(ListViewDragActionListener actionListener) {
		this.actionListener = actionListener;
	}
	
	public void setDragExtender(ListViewDragExtender extender) {
		this.dragExtender = extender;
	}
	
	public View getParentView() {
		return(this.parentView);
	}
	
	public View getView() {
		this.root = (ViewGroup) this.inflater.inflate(R.layout.update_test2, null);
		this.root.setOnClickListener(this);
		
		this.tvStdUpdateText = (TextView) this.root.findViewById(R.id.tvStdUpdateText);
		this.llSpace = (LinearLayout) this.root.findViewById(R.id.llSpace);
		
		this.pgStdUpdate = (ProgressBar) this.root.findViewById(R.id.pgStdUpdate);
		this.pgStdUpdate.setVisibility(View.GONE);
		
		this.imgDown = (ImageView) this.root.findViewById(R.id.imgDown);
		this.imgDown.setVisibility(View.VISIBLE);
		
		//this.bufferPx = (int) (this.tvStdUpdateText.getTextSize()*(-1)*2.5);
		this.bufferPx = (int) ((-1)*(this.tvStdUpdateText.getTextSize() + UIUtils.dp2px(this.context.getResources().getDisplayMetrics().density, 10)*2));
		
		MyLog.d(tag, "textSize=" + this.tvStdUpdateText.getTextSize() + "px");
		MyLog.d(tag, "bufferPx=" + this.bufferPx + "px");
		
		this.root.setY(this.bufferPx);
		
		return(this.root);
	}
	
	public void setY(float y) {
		if (this.root != null) {
			this.root.setY(y);	
		}
		
	}
	
	public void hideLayout() {

		if (GeneralManager.isUiThread() == true) {
			this.run();
			
		} else {
			Activity a = (Activity) this.context;
			a.runOnUiThread(this);
			
		}
	}
	
	private void updateCurrentStage() {
		switch (this.currentStage) {
			case 0:
				this.imgDown.setVisibility(View.VISIBLE);
				this.imgDown.setImageBitmap(this.bmpIcon[0]);
				
				this.tvStdUpdateText.setText(T_UPDATE);
				this.pgStdUpdate.setVisibility(View.GONE);
				break;
				
			case 1:
				this.imgDown.setVisibility(View.VISIBLE);
				this.imgDown.setImageBitmap(this.bmpIcon[1]);
				this.tvStdUpdateText.setText(T_RELEASE);
				this.pgStdUpdate.setVisibility(View.GONE);
				break;
				
			case 2:
				
				this.imgDown.setVisibility(View.GONE);
				this.tvStdUpdateText.setText(T_UPDATING);
				this.pgStdUpdate.setVisibility(View.VISIBLE);
				
		}
	}
	
	public int getCurrentStage() {
		return(this.currentStage);
	}

	@Override
	public void onPulling(int deltaY) {
		
		if (this.root != null) {
			
			if (this.bufferPx+deltaY > this.px ) {
				// Full text is in display

				if (this.tvStdUpdateText.getText().toString().equals(T_UPDATE)) {
					// Drag very fast, need to check text instead
						
					//this.llSpace.getLayoutParams().height = UIUtils.dp2px(this.context.getResources().getDisplayMetrics().density, this.dp2ChangeStatus);
					//this.llSpace.requestLayout();
						
					this.currentStage = 1;
					this.updateCurrentStage();
						
				}
					
				if (this.root.getY() != 0) {
					this.root.setY(0);
				}
					
				//return;
								
				this.root.setY(0);
				this.llSpace.getLayoutParams().height = this.bufferPx+deltaY;
				this.llSpace.requestLayout();
				
				this.currentStage = 1;
				this.updateCurrentStage();
			
			} else {
				this.llSpace.getLayoutParams().height = 0;
				this.llSpace.requestLayout();
				this.currentStage = 0;
			
				this.updateCurrentStage();
				this.root.setY(this.bufferPx+deltaY);
				MyLog.d(tag, "here, deltay=" + deltaY + ", root.getY()=" + this.root.getY());
			}
						
		}
		
		
	}

	@Override
	public void onRelease() {
		if (this.root != null) {
			
			switch (this.currentStage) {
				case 0:
					this.parentView.setVisibility(View.GONE);
					this.root.setY(this.bufferPx);
					
					//this.llSpace.getLayoutParams().height = this.bufferPx;
					this.llSpace.getLayoutParams().height = 0;
					this.llSpace.requestLayout();
					
					this.currentStage = 0;
					this.updateCurrentStage();
					break;
									
				case 1:
					this.currentStage = 2;
					this.updateCurrentStage();
										
					this.root.setY(0);
					this.llSpace.getLayoutParams().height = UIUtils.dp2px(this.context.getResources().getDisplayMetrics().density, this.topDPwhenUpdating);
					this.llSpace.requestLayout();
										
					//this.parentView.getLayoutParams().height = Math.abs(this.bufferPx)+50;
					this.parentView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
					this.parentView.requestLayout();
					
					if (this.actionListener != null) {
						this.actionListener.onUpdating(this);
					}
					
					break;
					
			}
			
		}
	}
	
	public boolean isCancelOnClick() {
		return(this.cancelOnClick);
	}
	
	public void setCancelOnClick(boolean b) {
		this.cancelOnClick = b;
	}

	@Override
	public void onClick(View v) {
		if (this.cancelOnClick == true) {
			this.hideLayout();
			if (this.actionListener != null) {
				this.actionListener.onCancelled(this);
			}
		}
		
		
	}

	@Override
	public void run() {
		this.root.setY(this.bufferPx);
		this.parentView.setVisibility(View.GONE);
		this.llSpace.getLayoutParams().height = 0;
		this.llSpace.requestLayout();
		this.currentStage = 0;
		this.updateCurrentStage();		
	}
	
}
